package com.ruoyi.life.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.life.domain.LifeApartmentPerson;
import com.ruoyi.life.mapper.LifeApartmentPersonMapper;
import com.ruoyi.life.service.ILifeApartmentPersonService;
import com.ruoyi.omp.api.domain.BuiRoom;
import com.ruoyi.omp.api.domain.NyMeterAccount;
import com.ruoyi.system.api.domain.RemoteSysBmType;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 公寓人员Service业务层处理
 *
 * @author cjie
 * @date 2022-11-26
 */
@Slf4j
@Service
public class LifeApartmentPersonServiceImpl implements ILifeApartmentPersonService {
    @Autowired
    private LifeApartmentPersonMapper lifeApartmentPersonMapper;

    /**
     * 查询公寓人员
     *
     * @param id 公寓人员ID
     * @return 公寓人员
     */
    @Override
    public LifeApartmentPerson selectLifeApartmentPersonById(String id) {
        LifeApartmentPerson result = lifeApartmentPersonMapper.selectLifeApartmentPersonById(id);
        BuiRoom search = new BuiRoom();
        if(result != null && result.getCapacity() == null){
            search.setParkNum(result.getParkNum());
            search.setRoomType("公寓");
            search.setHouseNumber(result.getRoomNum());
            BuiRoom room = lifeApartmentPersonMapper.selectRoom(search);
            result.setCapacity(room.getCapacity());
        }
        return result;
    }

    /**
     * 查询公寓人员列表
     *
     * @param lifeApartmentPerson 公寓人员
     * @return 公寓人员
     */
    @Override
    public List<LifeApartmentPerson> selectLifeApartmentPersonList(LifeApartmentPerson lifeApartmentPerson) {
        BuiRoom search = new BuiRoom();
        search.setRoomType("公寓");
        List<LifeApartmentPerson> results = lifeApartmentPersonMapper.selectLifeApartmentPersonList(lifeApartmentPerson);
        results.stream().forEach(item->{
            if(item.getCapacity() == null){
                search.setParkNum(item.getParkNum());
                search.setHouseNumber(item.getRoomNum());
                BuiRoom room = lifeApartmentPersonMapper.selectRoom(search);
                if(room != null){
                    item.setCapacity(room.getCapacity());
                }else {
                    log.error(item.getParkNum() +" " + item.getRoomNum() + " 公寓不存在");
                }
            }
        });
        return results;
    }

    /**
     * 新增公寓人员
     *
     * @param lifeApartmentPerson 公寓人员
     * @return 结果
     */
    @Override
    public int insertLifeApartmentPerson(LifeApartmentPerson lifeApartmentPerson) {
        lifeApartmentPerson.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        lifeApartmentPerson.setCreateTime(DateUtils.getNowDate());
        lifeApartmentPerson.setCreateBy(SecurityUtils.getUsername());
        return lifeApartmentPersonMapper.insertLifeApartmentPerson(lifeApartmentPerson);
    }

    /**
     * 修改公寓人员
     *
     * @param lifeApartmentPerson 公寓人员
     * @return 结果
     */
    @Override
    public int updateLifeApartmentPerson(LifeApartmentPerson lifeApartmentPerson) {
        lifeApartmentPerson.setUpdateTime(DateUtils.getNowDate());
        lifeApartmentPerson.setUpdateBy(SecurityUtils.getUsername());
        return lifeApartmentPersonMapper.updateLifeApartmentPerson(lifeApartmentPerson);
    }

    /**
     * 批量删除公寓人员
     *
     * @param ids 需要删除的公寓人员ID
     * @return 结果
     */
    @Override
    public int deleteLifeApartmentPersonByIds(String[] ids) {
        return lifeApartmentPersonMapper.deleteLifeApartmentPersonByIds(ids);
    }

    /**
     * 删除公寓人员信息
     *
     * @param id 公寓人员ID
     * @return 结果
     */
    @Override
    public int deleteLifeApartmentPersonById(String id) {
        return lifeApartmentPersonMapper.deleteLifeApartmentPersonById(id);
    }

    @Override
    public List<BuiRoom> selectLifeApartmentByParkNum(String parkNum) {
        BuiRoom room = new BuiRoom();
        room.setParkNum(parkNum);
        room.setRoomType("公寓");
        List<BuiRoom> roomList = lifeApartmentPersonMapper.selectLifeApartmentList(room);
        return roomList;
    }

    @Override
    public String importData(List<LifeApartmentPerson> nyCableVOList, String meterType) {
        if (StringUtils.isNull(nyCableVOList) || nyCableVOList.size() == 0) {
            throw new CustomException("导入抄表数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        this.insertDataBatch(nyCableVOList,meterType);
        successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + nyCableVOList.size() + " 条，数据如下：");
        return successMsg.toString();
    }

    private void insertDataBatch(List<LifeApartmentPerson> nyCableVOList,String meterType){
        //批量插入数据
        List<LifeApartmentPerson> sysBmTypeExcelVOS = nyCableVOList.stream().filter(o -> StringUtils.isNotBlank(o.getPhone())).collect(Collectors.toList());
        for(LifeApartmentPerson apartmentPerson :sysBmTypeExcelVOS){
            //select
            LifeApartmentPerson result = lifeApartmentPersonMapper.selectLifeApartmentPersonByPhone(apartmentPerson.getPhone());
            //insert or update
            if(result == null){
                //insert
                this.insertLifeApartmentPerson(apartmentPerson);
            }else {
                //update
                apartmentPerson.setId(result.getId());
                this.updateLifeApartmentPerson(apartmentPerson);
            }
        }

    }

}
